﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mvc4Learn.Models;

namespace Mvc4Learn.Biz
{
    public class AccountBiz
    {
        public static bool Login(string userName, string password)
        {
            using (var db = new UserInfoContext())
            {
                var user = db.UserInfo.FirstOrDefault(x => x.UserName == userName);
                if (user != null && user.Password == password)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pass"></param>
        public static void Register(string userName, string pass, string email)
        {
            using (var db = new UserInfoContext())
            {
                if (IsExist(userName, db))
                    throw new Exception("该用户名已经注册！");
                UserInfo user = new UserInfo { UserName = userName, Password = pass, Email = email };
                db.UserInfo.Add(user);
                db.SaveChanges();
            }
        }

        private static bool IsExist(string userName, UserInfoContext db)
        {
            if (db.UserInfo.Any(x => x.UserName == userName))
                return true;
            return false;
        }
    }
}